package me.flyray.crm.core.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.wxiaoqi.security.biz.common.crm.request.DistributionRelationQueryRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.biz.BaseBiz;

import me.flyray.crm.core.entity.PersonalAccount;
import me.flyray.crm.core.entity.PersonalDistributionRelation;
import me.flyray.crm.core.mapper.PersonalAccountMapper;
import me.flyray.crm.core.mapper.PersonalDistributionRelationMapper;

/**
 * 个人分销关系
 *
 * @author centerroot
 * @email ${email}
 * @date 2018-07-16 10:15:49
 */
@Service
public class PersonalDistributionRelationBiz extends BaseBiz<PersonalDistributionRelationMapper, PersonalDistributionRelation> {
	
	private static final Logger log = LoggerFactory.getLogger(PersonalDistributionRelationBiz.class);
	
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	
	/**
	 * 列表
	 */
	public Map<String, Object> distributionRelations(DistributionRelationQueryRequest param) {
		log.info("查询分享关系，请求参数。。。{}"+param);
		Map<String, Object> respMap = new HashMap<>();
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("platformId", param.getPlatformId());
		reqMap.put("customerId", param.getCustomerId());
		try {
			List<Map<String, Object>> list = mapper.queryListWithCustomerId(reqMap);
			if (null != list && list.size() > 0) {
				for (Map<String, Object> map : list) {
					String platformId = (String) map.get("platformId");
					String personalId = (String) map.get("personalId");
					PersonalAccount personalAccountReq = new PersonalAccount();
					personalAccountReq.setPlatformId(platformId);
					personalAccountReq.setPersonalId(personalId);
					//火源
					personalAccountReq.setAccountType("ACC010");
					PersonalAccount personalAccount1 = personalAccountMapper.selectOne(personalAccountReq);
					if (null != personalAccount1) {
						map.put("force", personalAccount1.getAccountBalance());
					}else {
						map.put("force", "0");
					}
					//火钻
					personalAccountReq.setAccountType("ACC011");
					PersonalAccount personalAccount2 = personalAccountMapper.selectOne(personalAccountReq);
					if (null != personalAccount2) {
						map.put("diamond", personalAccount2.getAccountBalance());
					}else {
						map.put("diamond", "0");
					}
					//原力值
					personalAccountReq.setAccountType("ACC012");
					PersonalAccount personalAccount3 = personalAccountMapper.selectOne(personalAccountReq);
					if (null != personalAccount3) {
						map.put("source", personalAccount3.getAccountBalance());
					}else {
						map.put("source", "0");
					}
				}
			}
			respMap.put("code", BizResponseCode.OK.getCode());
			respMap.put("message", BizResponseCode.OK.getMessage());
			respMap.put("list", list);
		} catch (Exception e) {
			log.info("查询分享关系, 异常。。。"+e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		}
		return respMap;
	}
}